﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Constants;




namespace Budget
{
    public partial class FormIncome : Form
    {
        BindingList<string> m_bindingListType;
        BindingList<string> m_bindingListCategory;
        DataTable m_IncomeDataTable;
        BudgetFunctions m_BudgetFunctions;

        public FormIncome(ref DataSet i_IncomeDataSet)
        {
            InitializeComponent();
            m_BudgetFunctions = new BudgetFunctions();
            GetComboBoxesValues();
            m_IncomeDataTable = i_IncomeDataSet.Tables[Tables.Income];
            m_BudgetFunctions.AutoCompleteText(ref textBoxDescription, m_IncomeDataTable.DefaultView.ToTable(true, Fields.Description),Fields.Description);
        }
        

        private void GetComboBoxesValues()
        {
            m_BudgetFunctions.UpdateComboboxValues(ref m_bindingListType, "IncomeType", ref this.comboBoxType, "Transfer");
            m_BudgetFunctions.UpdateComboboxValues(ref m_bindingListCategory, "IncomeCategory", ref this.comboBoxCategory, "Salary");

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addTypeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = null;

            if (InputBox.Show("Add New Type", "Enter New Type", ref value) == System.Windows.Forms.DialogResult.OK)
            {
                m_bindingListType.Add(value);
            }
        }

        private void removeTypeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormRemoveCategory formRemoveCategory = new FormRemoveCategory(m_bindingListType);
            if(formRemoveCategory.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                DialogResult result = MessageBox.Show("Are you sure you want to remove " + m_bindingListType.ElementAt(formRemoveCategory.RemoveAtIndex()) + " Type?", "Remove Type", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    m_bindingListType.RemoveAt(formRemoveCategory.RemoveAtIndex());
                }
            }
        }

        private void addCategoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = null;

            if (InputBox.Show("Add New Category", "Enter New Category", ref value) == System.Windows.Forms.DialogResult.OK)
            {
                m_bindingListCategory.Add(value);
            }
        }

        private void removeCategoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormRemoveCategory formRemoveCategory = new FormRemoveCategory(m_bindingListCategory);
            if (formRemoveCategory.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DialogResult result = MessageBox.Show("Are you sure you want to remove " + m_bindingListCategory.ElementAt(formRemoveCategory.RemoveAtIndex()) + " Type?", "Remove Type", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    m_bindingListCategory.RemoveAt(formRemoveCategory.RemoveAtIndex());
                }
            }
        }

        private void FormIncome_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_BudgetFunctions.SaveComboBoxToXmlFile(m_bindingListType, "IncomeType");
            m_BudgetFunctions.SaveComboBoxToXmlFile(m_bindingListCategory, "IncomeCategory");
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                m_IncomeDataTable.Rows.Add(this.BuildIncomeRow());
                MessageBox.Show("New Income added succesfully");

                this.ClearFormValues();
            }
            catch (FormatException)
            {
                MessageBox.Show("Wrong data in amount field: " + textBoxAmount.Text);
                textBoxAmount.Clear();
            }
            catch(ArgumentNullException i_nullException)
            {
                MessageBox.Show(i_nullException.Message);
                if (i_nullException.Equals("textBoxDescription"))
                {
                    textBoxDescription.Clear();
                }
            }
            this.DialogResult = System.Windows.Forms.DialogResult.None;
        }

        private void ClearFormValues()
        {
            textBoxAmount.Clear();
            textBoxDescription.Clear();
            this.comboBoxCategory.SelectedIndex = this.comboBoxCategory.Items.IndexOf("Salary");
            this.comboBoxType.SelectedIndex = this.comboBoxType.Items.IndexOf("Transfer");
        }

        private DataRow BuildIncomeRow()
        {
            DataRow newIncomeRow = m_IncomeDataTable.NewRow();
            newIncomeRow[Fields.Date] = dateTimePicker1.Value.Date.ToShortDateString();

            newIncomeRow[Fields.Description] = ValidateDescriptionData();
            
            newIncomeRow[Fields.User] = ValidateUserData();

            newIncomeRow[Fields.Category] = comboBoxCategory.Text;
            newIncomeRow[Fields.Type] = comboBoxType.Text;

            newIncomeRow[Fields.Amount] = ValidateAmountData();

            return newIncomeRow;
        }

        private double ValidateAmountData()
        {
            return double.Parse(textBoxAmount.Text);
        }

        private string ValidateUserData()
        {
            if (radioButtonOfri.Checked == false && radioButtonRefael.Checked == false)
            {
                throw new ArgumentNullException("radioButtons", "Please select a user");
            }
            return radioButtonOfri.Checked ? Users.Ofri : Users.Refael;
        }

        private string ValidateDescriptionData()
        {
            if (!String.IsNullOrEmpty(textBoxDescription.Text))
            {
               return textBoxDescription.Text;
            }
            else
            {
                throw new ArgumentNullException("textBoxDescription", "Please enter Income Description");
            }
        }
    }
}
